package ca.cbc.medialib.service;


import com.theplatform.data.api.objects.type.Duration;

import ca.cbc.medialib.exception.ThePlatformObjectNotFoundException;

/**
 * =============================================
 * Copyright (c) 2011 Canadian Broadcasting Corporation.
 * All rights reserved.
 * <p/>
 * ca.cbc.medialib.service.EntityService.java
 * =============================================
 * <p/>
 * PURPOSE:	Superclass for communicate with thePlatform services
 * <p/>
 * =============================================
 * MODIFICATION		LOG	DATE		REASON / Change Request #
 * Liu Yang			Jun 22, 2011	Initial file creation
 * =============================================
 */

public abstract class EntityService {

    public abstract Object get(long id) throws ThePlatformObjectNotFoundException;

    protected String removePrefix(String str) {
        if (str != null && str.contains("/")) {
            String[] tmpArray = str.split("/");
            str = tmpArray[tmpArray.length - 1];
        }

        return str;
    }
    
    protected String getDurationAsString(Duration d) {
        String duration;
        duration = d.getHour() + ":" +
                singleDigitTimeWrapper(d.getMinute()) + ":" +
                singleDigitTimeWrapper(d.getSecond());
        return duration;
    }

    protected long zeroOrLong(String str) {
        long num = 0;
        if (str != null && !str.equals(""))
            num = Long.parseLong(str);

        return num;
    }

    protected String nullOrString(Object object) {
        if (object == null || object.toString().equals("(not specified)") || object.toString().equals(""))
            return null;
        else
            return object.toString();
    }

    protected String singleDigitTimeWrapper(long n) {
        if (n < 10 && n >= 0)
            return "0" + n;
        else
            return String.valueOf(n);
    }
}
